package com.midian.controller.banner;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;

import com.midian.model.banner.Banner;
import com.midian.model.push.PushMsg;
import com.midian.service.advice.IAdviceService;
import com.midian.service.banner.IBannerService;
import com.midian.service.push.IPushMsgService;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.midian.common.dao.OrderHelper;
import com.midian.common.filter.annotation.Validate;
import com.midian.common.pager.Pager;
import com.midian.common.system.ErrorConstants;
import com.midian.common.system.RetManager;
import com.midian.common.system.SysStatic;
import com.midian.common.utils.ContentJSONObject;
import com.midian.common.utils.ObjUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 消息推送控制器
 *
 */
@Controller("bannerController")
public class BannerController {
	/**日志*/
	private static Logger LOG = Logger.getLogger(BannerController.class);
	/**反馈意见业务*/
	private IBannerService bannerService;

	@Resource(name="bannerService")
	public void setBannerService(IBannerService bannerService) {
		this.bannerService = bannerService;
	}
	

	@Validate(retCode = RetManager.common_error)
	@RequestMapping(value = "/banner", method = RequestMethod.GET)
	public @ResponseBody Object banner(String client_key
			 ) throws Exception {
		JSONObject json = new JSONObject();
		try {
			Pager pager = new Pager();
			List<OrderHelper> orderHelpers = new ArrayList<OrderHelper>();
			OrderHelper orderHelper = new OrderHelper();
			orderHelper.setPropertyName("createTime");
			orderHelper.setType(OrderHelper.Type.DESC);
			orderHelpers.add(orderHelper);
			Banner target = new Banner();
			target.setDelFlag(SysStatic.NORMALDELFLAG);
			List<Banner> banners = bannerService.showList(target, pager, orderHelpers);
			JSONArray contentArray = new JSONArray();
			if(!banners.isEmpty()){
				for(Banner banner:banners){
					ContentJSONObject jsonObject = new ContentJSONObject();
					jsonObject.put("banner_id", banner.getId());
					jsonObject.put("pic_name",banner.getPicId());
					jsonObject.put("activity_id", banner.getActivityId());
					contentArray.add(jsonObject.getContent());
				}
			}
			json.put("content", contentArray);
		} catch (Exception e) {
			LOG.error(e.getMessage());
		    throw e;
		}
		return json;
	}
}
